﻿using Sunny.UI;

namespace nhLib.Extensions;

internal static class UIDataGridViewExtension
{
    /// <summary>
    /// DataGridView加列
    /// </summary>
    /// <typeparam name="T">DataGridViewColumn的子类</typeparam>
    /// <param name="propertyName">绑定的属性</param>
    /// <param name="headerText">列标题</param>
    /// <param name="contentAlignment">内容位置</param>
    /// <param name="format">格式</param>
    /// <param name="fillWeight">宽带比重</param>
    /// <param name="Width">宽度</param>
    /// <returns></returns>
    public static T NewColumn<T>(this UIDataGridView udgv, string propertyName, string headerText,
        DataGridViewContentAlignment? contentAlignment = null, string? format = null, int fillWeight = 100, int Width = 100) where T : DataGridViewColumn, new()
    {
        T column = new()
        {
            Name = propertyName,
            DataPropertyName = propertyName,
            HeaderText = headerText,
            FillWeight = fillWeight,
            Width = Width,            
        };

        if (contentAlignment != null)
        {
            column.DefaultCellStyle.Alignment = (DataGridViewContentAlignment)contentAlignment;
        }

        if (format != null)
        {
            column.DefaultCellStyle.Format = format;
        }

        udgv.Columns.Add(column);

        return column;
    }

    /// <summary>
    /// DataGridView加列
    /// </summary>
    /// <typeparam name="T">DataGridViewColumn的子类</typeparam>
    /// <param name="propertyName">绑定的属性</param>
    /// <param name="contentAlignment">内容位置</param>
    /// <param name="format">格式</param>
    /// <param name="fillWeight">宽带比重</param>
    /// <param name="Width">宽度</param>
    /// <returns></returns>
    public static T NewColumn<T>(this UIDataGridView udgv, string propertyName, DataGridViewContentAlignment? contentAlignment = null, 
        string? format = null, int fillWeight = 100, int Width = 100) where T : DataGridViewColumn, new()
    {
        T column = new()
        {
            Name = propertyName,
            DataPropertyName = propertyName,
            FillWeight = fillWeight,
            Width = Width,
        };

        if (contentAlignment != null)
        {
            column.DefaultCellStyle.Alignment = (DataGridViewContentAlignment)contentAlignment;
        }

        if (format != null)
        {
            column.DefaultCellStyle.Format = format;
        }

        udgv.Columns.Add(column);

        return column;
    }
}
